package stardust;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import stardust.core.log.Log;


public abstract class Stardust
{

  public static final String NAME = "Stardust";

  public static final String VERSION = "0";

  public static final String SIGNATURE = "$" + NAME + "$" + VERSION + "$";

  public static final Properties PARAMETER = new Properties();

  static
  {
    try
    {
      Stardust.PARAMETER.load(Stardust.loadResourceAsInputStream("stardust.properties"));
    }
    catch(IOException e)
    {
      Log.global.warning("无法加载Stardust配置文件。", e);
    }
  }

  public static String getStringParameter(String name, String defaultValue)
  {
    return Stardust.PARAMETER.getProperty(name) == null ? defaultValue : Stardust.PARAMETER.getProperty(name);
  }

  public static int getIntegerParameter(String name, int defaultValue)
  {
    return Stardust.PARAMETER.getProperty(name) == null ? defaultValue : Integer.parseInt(Stardust.PARAMETER.getProperty(name));
  }

  public static void idle(long ms)
  {
    try
    {
      Thread.sleep(ms);
    }
    catch(InterruptedException e)
    {
      ;
    }
  }

  public static InputStream loadResourceAsInputStream(String path)
  {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);

    try
    {
      if (in == null)
      {
        in = new FileInputStream(new File(path));
      }
    }
    catch(Exception e)
    {
      try
      {
        in = new URL(path).openStream();
      }
      catch(Exception ex)
      {
        in = null;
      }
    }

    return in;
  }

}
